/**
 * @Project : AlgorithmLearning
 * @Package : ACWing._2LanQiao._7Greedy
 * @File : Q1055.java
 * @Author : WangRuoyu
 * @Date 2023/3/19 23:31
 */

package acwing._2LanQiao._7Greedy;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @Project : AlgorithmLearning
 * @Package : ACWing._2LanQiao._7Greedy
 * @File : Q1055.java
 * @Author : WangRuoyu
 * @Date 2023/3/19 23:31
 */

public class Q1055 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] words = br.readLine().split(" ");

        int n = Integer.parseInt(words[0]);
        words = br.readLine().split(" ");
        int res = 0;
        for (int i = 0; i < n - 1; i++) {
            int ii = Integer.parseInt(words[i]);
            int iii = Integer.parseInt(words[i + 1]);
            if (iii > ii) {
                res += iii - ii;
            }
        }
        System.out.println(res);
    }
}

